package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"product-payment/models/mysql"

	"product-payment/prorpc/internal/svc"
	"product-payment/prorpc/prorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopListLogic {
	return &ShopListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopListLogic) ShopList(in *prorpc.ShopListRequest) (*prorpc.ShopListResponse, error) {
	// todo: add your logic here and delete this line

	var shops mysql.Shop
	mysql.DB.Debug().Where("id = ?", shops.ID).Find(&shops)

	if shops.ID == 0 {
		return nil, errors.New("当前商品不存在")
	}

	shops = mysql.Shop{
		Model: gorm.Model{ID: uint(in.ShopId)},
	}
	mysql.DB.Debug().Find(&shops)

	return &prorpc.ShopListResponse{
		Name:  shops.Name,
		Price: float32(shops.Price),
		Num:   int64(shops.Num),
	}, nil
}
